package com.just.bz_back.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.just.bz_back.dao.PartAddMapper;
import com.just.bz_back.dao.PartMapper;
import com.just.bz_back.dao.PartUseMapper;
import com.just.bz_back.model.*;
import com.just.bz_back.model.dto.Page;
import com.just.bz_back.service.PartService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 此类需要添加事务
 */
@Service
public class PartServiceImpl implements PartService {

    @Resource
    private PartMapper partMapper;
    @Resource
    private PartAddMapper partAddMapper;
    @Resource
    private PartUseMapper partUseMapper;
    @Override
    public int CreatePart(Part part) {
        //1.查询数据库是否有此备件
        PartExample partExample=new PartExample();
        partExample.createCriteria().andNameEqualTo(part.getName());
        List<Part> partList=partMapper.selectByExample(partExample);
        int partId;
        if(partList.size() == 0)
        {
            //2.没有则插入数据
           partMapper.insertSelective(part);
            partId =part.getId();
        }
        else
        {
            //3.有则更新数据
            part.setNumber(part.getNumber()+partList.get(0).getNumber());
            partMapper.updateByExampleSelective(part,partExample);
            partId=partList.get(0).getId();
        }
        //4.插入part_add表数据
        PartAdd partAdd = part.getPartAdd();
        partAdd.setPartId(partId);
        return partAddMapper.insertSelective(partAdd);
    }

    @Override
    public PageInfo<Part> ListPart(Page page) {
        PageHelper.startPage(page.getPageNum(),page.getPageSize());
        List<Part> partList=partMapper.selectByExample(null);
        PageInfo<Part> partPageInfo = new PageInfo<>(partList);
        return partPageInfo;
    }

    @Override
    public List<Part> PartListAll() {

        return partMapper.selectByExample(null);
    }

    @Override
    public PageInfo<PartAdd> PartAddList(PartAdd partAdd) {
        Page page=partAdd.getPage();
        PageHelper.startPage(page.getPageNum(),page.getPageSize());
        PartAddExample partAddExample=new PartAddExample();
        PartAddExample.Criteria criteria = partAddExample.createCriteria();
        if (partAdd.getAdminId() != null)
        {
            criteria.andAdminIdEqualTo(partAdd.getAdminId());
        }
        if (partAdd.getPartId() != null)
        {
            criteria.andPartIdEqualTo(partAdd.getPartId());
        }
        List<PartAdd> partAddList=partAddMapper.selectByExample(partAddExample);
        PageInfo<PartAdd> partAddPageInfo = new PageInfo<>(partAddList);
        return partAddPageInfo;
    }

    @Override
    public PageInfo<PartUse> PartUseList(PartUse partUse) {
        Page page=partUse.getPage();
        PageHelper.startPage(page.getPageNum(),page.getPageSize());
        PartUseExample partUseExample=new PartUseExample();
        PartUseExample.Criteria criteria = partUseExample.createCriteria();
        if (partUse.getAdminId() != null)
        {
            criteria.andAdminIdEqualTo(partUse.getAdminId());
        }
        if (partUse.getPartId() != null)
        {
            criteria.andPartIdEqualTo(partUse.getPartId());
        }
        List<PartUse> partUseList=partUseMapper.selectByExample(partUseExample);
        PageInfo<PartUse> partUsePageInfo = new PageInfo<>(partUseList);
        return partUsePageInfo;
    }
}
